<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>portal</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
    	<blockquote class="layui-elem-quote">如果插件应用安装不了，请先进行环境预热：php ./bin/phalapi-plugin-prepare.php，或者直接使用命令安装：<code>php ./bin/phalapi-plugin-install.php</code>，或<a href="https://www.phalapi.net/">联系我们PhalApi</a>。</blockquote>
        
    	<blockquote class="layui-elem-quote">如果你想开发自己的插件和应用，请参考官方开发文档：<a href="http://docs.phalapi.net/#/v2.0/how-to-dev-plugin">第三方应用插件开发教程</a>。</blockquote>
		
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    	
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-danger layui-btn-xs data-count-edit" lay-event="edit">安装</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs data-count-edit" lay-event="uninstall"><i class="layui-icon"></i> 卸载</a>
        </script>

    </div>
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '/?s=Portal.Plugins.GetMinePlugins', // 换成相应的运营平台接口
            //toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.ret == 200 ? 0 : res.ret, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.items //解析数据列表
                };
            },
            cols: [[
                {type: "checkbox", width: 50, fixed: "left"},
                {field: 'plugin_key', minWidth: 50, title: '插件编号', sort: true},
                {field: 'plugin_name', minWidth: 50, title: '插件名称'},
                {field: 'plugin_author', minWidth: 50, title: '开发者'},
                {field: 'plugin_version', minWidth: 50, title: '版本'},
                {
                    field: 'plugin_status', minWidth: 50, align: 'center', templet: function (d) {
                        if (d.plugin_status == 1) {
                            return '<span class="layui-badge layui-bg-green">已安装</span>';
                        } else {
                            return '<span class="layui-badge-rim layui-bg-red">待安装</span>';
                        }
                    }, title: '插件状态', sort: true
                },
                {title: '操作', minWidth: 50, templet: '#currentTableBar', fixed: "right", align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true
        });
        

        /**
         * toolbar监听事件
         */
        table.on('tool(currentTableFilter)', function (obj) {
            if (obj.event === 'edit') {  // 监听添加操作
            	var doInstallFun = function(plugin_key) {
                    $.ajax({
                        url: '/?s=Portal.Plugins.Install',
                        type:'post',
                        data: {plugin_key: plugin_key},
                        success:function(data){
                            if (data.ret != 200) {
                                layer.msg(data.msg);
                                return;
                            }

                            layer.open({
                            	  title: (data.data.install_result ? '插件安装完成' : '插件安装失败')
                                  , area: ['600px', '500px']
                            	  ,content: data.data.detail
                            });     
                            	  
                        },
                        complete: function () {
                      /*   	layer.open({
                          	  title: '插件似乎安装不了？'
                          	  ,content: '如果缺少权限，会导致安装失败，可以使用命令直接安装：<br/>php ./bin/phalapi-install-plugin.php ' + obj.data.plugin_key 
                          });  */
                        }
                    });
            	}
            
            	if (obj.data.plugin_status == 1) {
            		layer.msg('插件已安装，不需要重复安装~');
            		//return false;

                    layer.confirm("插件已安装，是否需要重新安装？<br/><br/>重装安装可能会删除当前插件应用的旧数据，并且覆盖原来插件的代码。", function (index) {
                    	doInstallFun(obj.data.plugin_key);
                        layer.close(index);
                    });
            	} else {
            		doInstallFun(obj.data.plugin_key);
            	}
            } else if (obj.event == 'uninstall') {
                layer.confirm(obj.data.plugin_name + "<br/>插件已安装，是否需要狠心卸载？<br/><br/>卸载后会删除安装的插件代码，保留数据库数据及安装包，如有需要可重新安装。", function (index) {
                    $.ajax({
                        url: '/?s=Portal.Plugins.Uninstall',
                        type:'post',
                        data: {plugin_key: obj.data.plugin_key},
                        success:function(data){
                            if (data.ret != 200) {
                                layer.msg(data.msg);
                                return;
                            }

                            layer.open({
                            	  title: (data.data.uninstall_result ? '插件卸载完成' : '插件卸载失败')
                                  , area: ['600px', '500px']
                            	  ,content: data.data.detail
                            });     
                            	  
                        }
                    });
                })
            }
        });
    });
</script>
<script>

</script>

</body>
</html>
